﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
    /// <summary>
    /// 数据访问类:Collect
    /// </summary>
    public partial class Collect
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsGoods(int 商品ID,string type)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Collect");
            strSql.Append(" where 商品ID=@商品ID and 收藏类别=@type");
            SqlParameter[] parameters = {
					new SqlParameter("@商品ID", SqlDbType.Int,4),
                    new SqlParameter("@type", SqlDbType.NVarChar,20)
            };
            parameters[0].Value = 商品ID;
            parameters[1].Value = type;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 收藏记录
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public DataTable GetCollects(string user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(
                @"SELECT a.收藏ID,a.商品ID,a.收藏时间,a.收藏类别,CASE WHEN a.收藏类别='hot' THEN '热门推荐' ELSE 
                            CASE WHEN a.收藏类别='sale' THEN '特价专区' ELSE '积分兑换' END END AS 收藏地点,
                            b.商品名称,b.商品品牌,
                            CASE WHEN a.收藏类别='hot' THEN b.原价 ELSE 
                            CASE WHEN a.收藏类别='sale' THEN b.现价 ELSE '0.00' END END AS 单价,
                            CASE WHEN a.收藏类别='score' THEN b.兑换积分 ELSE 0 END AS 兑换积分,
                            CASE WHEN a.收藏类别='score' THEN 0 ELSE b.赠送积分 END AS 赠送积分
                             FROM dbo.Collect a
                            LEFT JOIN dbo.GoodsInfo b 
                            ON a.商品ID=b.商品ID");
            strSql.Append(" where a.用户ID=@用户ID");
            SqlParameter[] parameters = {
					new SqlParameter("@用户ID", user)
            };
            return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
        }
    }
}

